package com.h3c.gm.controller

import com.google.common.collect.Maps
import com.h3c.gm.common.Response
import com.h3c.gm.entity.DO.SysOrgQuotaDO
import com.h3c.gm.service.BusHostSpecService
import com.h3c.gm.service.SysOrgQuotaService
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController

import javax.annotation.Resource

/**
 * Created by d14287 on 2017/7/1.
 */
@RestController
class SysOrgQuotaController {
    @Resource
    BusHostSpecService busHostSpecService
    @Resource
    BusStorageSpecController busStorageSpecController
    @Resource
    SysOrgQuotaService sysOrgQuotaService

    @RequestMapping(value = 'quota/orgkey/{orgkey}/qtype/{qtype}', method = RequestMethod.GET)
    def cpuQuotaStatistics(@PathVariable(name = 'orgkey') String orgkey, @PathVariable(name = 'qtype') String qtype) {
        def resultMap = Maps.newHashMap()
        String type
        if ('host'.equals(qtype)) {
            type = 'instances'
        } else if ('cpu'.equals(qtype)) {
            type = 'cores'
        } else if ('ram'.equals(qtype)) {
            type = 'ram'
        } else if ('storage'.equals(qtype)) {
            type = 'volumes'
        }
        def sysOrgQuotaDO = sysOrgQuotaService.getOrgQuota(orgkey, type)
        resultMap.put('quotaUsed', sysOrgQuotaDO == null ? null : sysOrgQuotaDO.getQuotaused())
        resultMap.put('quotaUnused', sysOrgQuotaDO == null ? null : sysOrgQuotaDO.getQuotacount() - sysOrgQuotaDO.getQuotaused())
        resultMap.put('deptQuota', sysOrgQuotaDO == null ? null : sysOrgQuotaDO.getDeptquota())
        resultMap.put('count', sysOrgQuotaDO == null ? null : sysOrgQuotaDO.getQuotacount())
        Response.instance().ofSuccess(resultMap)
    }
}
